package main

import (
	"apifast/api"
	"apifast/configs"
	"apifast/db"
	"log"
	"os"

	"github.com/kardianos/service"
)

func init() {
	// file := util.Public.Abs("/logs", time.Now().Format("2006-01-02")+".txt")
	// logFile, err := os.OpenFile(file, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0766)
	// if err != nil {
	// 	panic(err)
	// }
	// log.SetOutput(logFile) // 将文件设置为log输出的文件
	// log.SetFlags(log.LstdFlags | log.Lshortfile | log.LUTC)
}

type program struct {
}

func (p *program) Start(s service.Service) error {
	if err := db.Run(); err != nil {
		return err
	}
	return api.Run()
}

func (p *program) Stop(s service.Service) error {
	return api.Shutdown()
}

// @title Swagger Example API
// @version 1.0
// @description This is a sample server celler server.
// @termsOfService http://swagger.io/terms/
func main() {
	if err := configs.Load("config.yaml"); err != nil {
		log.Fatal(err)
	}
	svcfg := &service.Config{
		Name:        configs.AppName,
		DisplayName: configs.AppName,
		Description: "This is " + configs.AppName + " application",
	}
	s, err := service.New(&program{}, svcfg)
	if err != nil {
		log.Fatal(err)
	}
	if len(os.Args) > 1 {
		if os.Args[1] == "install" {
			err = s.Install()
		} else if os.Args[1] == "uninstall" {
			err = s.Uninstall()
		}
		log.Println(err)
		return
	}
	err = s.Run()
	if err != nil {
		log.Fatal(err)
	}
}
